package com.jcrj.service;

import com.jcrj.dao.ReportDao;
import com.jcrj.entity.OMemo;
import com.jcrj.entity.OMenu;
import com.jcrj.entity.OMyOpinion;
import com.jcrj.entity.OMyReports;
import com.jcrj.entity.OMyTakereport;
import com.jcrj.entity.OMyTemplate;
import com.jcrj.entity.OMyTemplateAllot;
import com.jcrj.entity.OUser;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class ReportService
{
  private ReportDao reportDao;

  
public List<OMenu> getQuickMenuList(int userId)
  {
    String sql = "select t.*,m.imgfile from O_MENU t,(select o.menu_id,o.imgfile,o.px from o_menu_quick o where o.my_user_id=" + userId + ") m where t.id=m.menu_id and t.id in(select o.menu_id from o_menu_quick o where o.my_user_id=" + userId + ") order by m.px asc";
    List<Object[]> list = new ArrayList<Object[]>();
    List<OMenu> menuList = new ArrayList<OMenu>();
    list = this.reportDao.findBySql(sql);
    for (Object[] obj : list) {
      OMenu menu = new OMenu();
      menu.setId(Integer.valueOf(((BigDecimal)obj[0]).intValue()));
      menu.setName((String)obj[1]);
      menu.setUrl((String)obj[2]);
      menu.setPid(Integer.valueOf(((BigDecimal)obj[3]).intValue()));
      menu.setPx(Integer.valueOf(((BigDecimal)obj[5]).intValue()));
      menu.setImgUrl((String)obj[6]);
      menuList.add(menu);
    }
    return menuList;
  }

  public void updateMemo(OMemo memo) {
    this.reportDao.updateReport(memo);
  }

  public OMemo getMemo(int id) {
    return (OMemo)this.reportDao.getObject(new OMemo(), id);
  }

  public void deleteMemo(String[] ids) {
    if (ids != null) {
      String id = "";
      for (int i = 0; i < ids.length; i++) {
        if (id == "")
          id = id + ids[i];
        else {
          id = id + "," + ids[i];
        }
      }
      String hql = "delete from OMemo where id in (" + id + ")";
      this.reportDao.executeHql(hql);
    }
  }

  public Long countMemo(int id, String name, String type) {
    String hql = "select count(id) from OMemo where myUserId=" + id + " and type='" + type + "'";
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and content like '%" + name.trim() + "%'";
    }
    return this.reportDao.countTotalByHql(hql);
  }

  public List<OMemo> getMemo(int id, int page, String name, String type) {
    String hql = "from OMemo where myUserId=" + id + " and type='" + type + "'";
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and content like '%" + name.trim() + "%'";
    }
    return this.reportDao.findByPage(hql, page);
  }

  public void saveMemo(OMemo oMemo) {
    this.reportDao.saveReport(oMemo);
  }

  public List getOMyTemplateList(int id, int userId) {
    String sql = "select distinct t.* from O_MY_TEMPLATE t,O_MY_TEMPLATE_ALLOT m where t.id=m.template_id and t.all_id=" + id + " or m.user_id=" + userId;
    return this.reportDao.findBySql(sql);
  }

  public OMyTemplate getTemplateNew(int id) {
    return (OMyTemplate)this.reportDao.getObject(new OMyTemplate(), id);
  }

  public void deleteTemplate(String[] ids) {
    if (ids != null) {
      String id = "";
      for (int i = 0; i < ids.length; i++) {
        if (id == "")
          id = id + ids[i];
        else {
          id = id + "," + ids[i];
        }
      }
      String hql = "delete from OMyTemplate where id in(" + id + ")";
      String hql_2 = "delete from OMyTemplateAllot where templateId in(" + id + ")";
      this.reportDao.executeHql(hql);
      this.reportDao.executeHql(hql_2);
    }
  }

  public Long countTemplate(String name, int id) {
    String hql = "select count(id) from OMyTemplate where myUserId=" + id;
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and name like '%" + name.trim() + "%'";
    }
    return this.reportDao.countTotalByHql(hql);
  }

  public List<OMyTemplate> getTemplateList(int page, String name, int id) {
    String hql = "from OMyTemplate where myUserId=" + id;
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and name like '%" + name.trim() + "%'";
    }
    return this.reportDao.findByPage(hql, page);
  }

  public void saveTemplate(String users, String name, String description, String content, int myUserId, String userName) {
    OMyTemplate myTemplate = new OMyTemplate();
    myTemplate.setName(name);
    if ((users != null) && (!"".equals(users)))
      myTemplate.setUserId(users);
    else {
      myTemplate.setAllId(Integer.valueOf(1));
    }
    myTemplate.setDescription(description);
    myTemplate.setContent(content);
    myTemplate.setMyUserId(Integer.valueOf(myUserId));
    myTemplate.setUserName(userName);
    this.reportDao.saveReport(myTemplate);
    if ((users != null) && (!"".equals(users))) {
      String[] ids = users.split(",");
      for (int i = 0; i < ids.length; i++) {
        OMyTemplateAllot myTemplateAllot = new OMyTemplateAllot();
        myTemplateAllot.setTemplateId(myTemplate.getId());
        myTemplateAllot.setUserId(Integer.valueOf(Integer.parseInt(ids[i])));
        this.reportDao.saveReport(myTemplateAllot);
      }
    } else {
      OMyTemplateAllot myTemplateAllot = new OMyTemplateAllot();
      myTemplateAllot.setTemplateId(myTemplate.getId());
      this.reportDao.saveReport(myTemplateAllot);
    }
  }

  public List<OMyOpinion> getOpinionList(String reportId) {
    String hql = "from OMyOpinion where myReportId=" + reportId + " order by addTime desc";
    return this.reportDao.findByHql(hql);
  }

  public void saveAgainReport(String reportid, String ids) {
    String[] id = ids.split(",");
    for (int i = 0; i < id.length; i++) {
      OMyTakereport oTakereport = new OMyTakereport();
      oTakereport.setOMyReportsId(Integer.valueOf(Integer.parseInt(reportid)));
      oTakereport.setUserId(Integer.valueOf(Integer.parseInt(id[i])));
      oTakereport.setState(Integer.valueOf(0));
      this.reportDao.saveReport(oTakereport);
    }
    OMyReports oMyReports = new OMyReports();
    oMyReports = (OMyReports)this.reportDao.getObject(new OMyReports(), Integer.parseInt(reportid));
    String ueid = oMyReports.getUserId() + "," + ids;
    String hql = "update OMyReports set userId='" + ueid + "' where id=" + reportid;
    this.reportDao.executeHql(hql);
  }

  public void saveReportOpinion(int userId, String content, String userName, String reportId) {
    OMyOpinion opinion = new OMyOpinion();
    opinion.setUserId(Integer.valueOf(userId));
    opinion.setUserName(userName);
    opinion.setMyReportId(Integer.valueOf(Integer.parseInt(reportId)));
    opinion.setContent(content);
    opinion.setAddTime(new Date());
    this.reportDao.saveReport(opinion);
  }

  public void updateTakeReport(int id, int stateid, int reid, String recId) {
    String hql = "update OMyTakereport set state=" + stateid + " where id=" + id;
    String hql_2 = "update OMyReports set control=" + reid + " where id=" + recId;
    this.reportDao.executeHql(hql);
    this.reportDao.executeHql(hql_2);
  }

  public void deleteTakeReport(String[] ids) {
    if (ids != null) {
      String id = "";
      for (int i = 0; i < ids.length; i++) {
        if (id == "")
          id = id + ids[i];
        else {
          id = id + "," + ids[i];
        }
      }
      String hql = "delete from OMyTakereport where id in (" + id + ")";
      this.reportDao.executeHql(hql);
    }
  }

  public OUser getUserNameNew(int id)
  {
    return (OUser)this.reportDao.getObject(new OUser(), id);
  }

  public Long countTakereport(int id, String name, String type, String startDate, String endDate) {
    String sql = "select m.id,t.id as ids,t.state,m.reporting_date,m.my_user_id,m.user_id,m.type from o_my_takereport t,o_my_reports m where t.o_my_reports_id=m.id and t.user_id=" + id;
    if ((name != null) && (!"".equals(name))) {
      sql = sql + " and m.content like '%" + name.trim() + "%'";
    }
    if ((type != null) && (!"".equals(type))) {
      sql = sql + " and m.type='" + type + "'";
    }
    if ((startDate != null) && (!"".equals(startDate)) && (endDate != null) && (!"".equals(endDate)))
      sql = sql + " and m.reporting_date >= to_date('" + startDate + "','yyyy-mm-dd') and  m.reporting_date<=to_date('" + endDate + "','yyyy-mm-dd')";
    else if ((startDate != null) && (!"".equals(startDate)))
      sql = sql + " and  m.reporting_date >= to_date('" + startDate + "','yyyy-mm-dd')";
    else if ((endDate != null) && (!"".equals(endDate))) {
      sql = sql + " and m.reporting_date <= to_date('" + endDate + "','yyyy-mm-dd')";
    }
    List<OMyReports> reportList = new ArrayList<OMyReports>();
    List<Object[]> list = this.reportDao.findBySql(sql);
    for (Object[] obj : list) {
      OMyReports report = new OMyReports();
      report.setId(Integer.valueOf(((BigDecimal)obj[0]).intValue()));
      report.setTakereportId(Integer.valueOf(((BigDecimal)obj[1]).intValue()));
      report.setState(Integer.valueOf(((BigDecimal)obj[2]).intValue()));
      report.setReportingDate((Date)obj[3]);
      report.setMyUserId(Integer.valueOf(((BigDecimal)obj[4]).intValue()));
      report.setUserId((String)obj[5]);
      report.setType((String)obj[6]);
      reportList.add(report);
    }
    return Long.valueOf(Long.valueOf(reportList.size()));
  }

  public List<OMyReports> getTakereportList(int id, String name, String type, String startDate, String endDate, int page) {
    String sql = "select m.id,t.id as ids,t.state,m.reporting_date,m.my_user_id,m.user_id,m.type from o_my_takereport t,o_my_reports m where t.o_my_reports_id=m.id and t.user_id=" + id;
    if ((name != null) && (!"".equals(name))) {
      sql = sql + " and m.content like '%" + name.trim() + "%'";
    }
    if ((type != null) && (!"".equals(type))) {
      sql = sql + " and m.type='" + type + "'";
    }
    if ((startDate != null) && (!"".equals(startDate)) && (endDate != null) && (!"".equals(endDate)))
      sql = sql + " and m.reporting_date >= to_date('" + startDate + "','yyyy-mm-dd') and  m.reporting_date<=to_date('" + endDate + "','yyyy-mm-dd')";
    else if ((startDate != null) && (!"".equals(startDate)))
      sql = sql + " and  m.reporting_date >= to_date('" + startDate + "','yyyy-mm-dd')";
    else if ((endDate != null) && (!"".equals(endDate))) {
      sql = sql + " and m.reporting_date <= to_date('" + endDate + "','yyyy-mm-dd')";
    }
    sql = sql + " order by m.reporting_date desc";
    List reportList = new ArrayList();
    List<Object[]> list = this.reportDao.findByPageSql(sql, page);
    for (Object[] obj : list) {
      OMyReports report = new OMyReports();
      report.setId(Integer.valueOf(((BigDecimal)obj[0]).intValue()));
      report.setTakereportId(Integer.valueOf(((BigDecimal)obj[1]).intValue()));
      report.setState(Integer.valueOf(((BigDecimal)obj[2]).intValue()));
      report.setReportingDate((Date)obj[3]);
      report.setMyUserId(Integer.valueOf(((BigDecimal)obj[4]).intValue()));
      report.setUserId((String)obj[5]);
      report.setType((String)obj[6]);
      reportList.add(report);
    }
    return reportList;
  }

  public void updateReport(int id, String users, String content, String note, String fileId, String type, int myUserId)
  {
    OMyReports oMyReports = new OMyReports();
    oMyReports.setId(Integer.valueOf(id));
    oMyReports.setUserId(users);
    oMyReports.setContent(content);
    oMyReports.setNote(note);
    oMyReports.setFileId(fileId);
    oMyReports.setType(type);
    oMyReports.setMyUserId(Integer.valueOf(myUserId));
    oMyReports.setReportingDate(new Date());
    this.reportDao.updateReport(oMyReports);
    String hql = "delete from OMyTakereport where OMyReportsId=" + id;
    this.reportDao.executeHql(hql);
    String[] useId = users.split(",");
    for (int i = 0; i < useId.length; i++) {
      OMyTakereport oMyTakereport = new OMyTakereport();
      oMyTakereport.setOMyReportsId(Integer.valueOf(id));
      oMyTakereport.setUserId(Integer.valueOf(Integer.parseInt(useId[i])));
      oMyTakereport.setState(Integer.valueOf(0));
      this.reportDao.saveReport(oMyTakereport);
    }
  }

  public OMyReports getReport(int id) {
    return (OMyReports)this.reportDao.getObject(new OMyReports(), id);
  }

  public List<OUser> getUserName(String[] id, String ids) {
    if (id != null) {
      ids = "";
      for (int i = 0; i < id.length; i++) {
        if (ids == "")
          ids = ids + id[i];
        else {
          ids = ids + "," + id[i];
        }
      }
    }
    String hql = "from OUser where id in (" + ids + ")";
    return this.reportDao.findByHql(hql);
  }

  public void deleteReport(String[] ids)
  {
    if (ids != null) {
      String id = "";
      for (int i = 0; i < ids.length; i++) {
        if (id == "")
          id = id + ids[i];
        else {
          id = id + "," + ids[i];
        }
      }
      String hql = "delete from OMyReports where id in(" + id + ")";
      String hql_2 = "delete from OMyTakereport where OMyReportsId in(" + id + ")";
      this.reportDao.executeHql(hql);
      this.reportDao.executeHql(hql_2);
    }
  }

  public void saveReport(String users, String content, String note, String fileId, String type, int myUserId, String tempName) {
    OMyReports or = new OMyReports();
    or.setUserId(users);
    or.setContent(content);
    or.setNote(note);
    or.setFileId(fileId);
    or.setType(type);
    or.setMyUserId(Integer.valueOf(myUserId));
    or.setReportingDate(new Date());
    or.setTemplateName(tempName);
    this.reportDao.saveReport(or);
    String[] userid = users.split(",");
    for (int i = 0; i < userid.length; i++) {
      OMyTakereport oMyTakereport = new OMyTakereport();
      oMyTakereport.setOMyReportsId(or.getId());
      oMyTakereport.setUserId(Integer.valueOf(Integer.parseInt(userid[i])));
      oMyTakereport.setState(Integer.valueOf(0));
      this.reportDao.saveReport(oMyTakereport);
    }
  }

  public Long countReport(int id, String name, String type, String startDate, String endDate) {
    String hql = "select count(id) from OMyReports where myUserId=" + id;
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and content like '%" + name.trim() + "%'";
    }
    if ((type != null) && (!"".equals(type))) {
      hql = hql + " and type='" + type + "'";
    }
    if ((startDate != null) && (!"".equals(startDate)) && (endDate != null) && (!"".equals(endDate)))
      hql = hql + " and reportingDate >= to_date('" + startDate + "','yyyy-mm-dd') and reportingDate<=to_date('" + endDate + "','yyyy-mm-dd')";
    else if ((startDate != null) && (!"".equals(startDate)))
      hql = hql + " and reportingDate >= to_date('" + startDate + "','yyyy-mm-dd')";
    else if ((endDate != null) && (!"".equals(endDate))) {
      hql = hql + " and reportingDate<=to_date('" + endDate + "','yyyy-mm-dd')";
    }
    return this.reportDao.countTotalByHql(hql);
  }

  public List<OMyReports> reportList(int id, String name, String type, String startDate, String endDate, int page) {
    String hql = "from OMyReports where myUserId=" + id;
    if ((name != null) && (!"".equals(name))) {
      hql = hql + " and content like '%" + name.trim() + "%'";
    }
    if ((type != null) && (!"".equals(type))) {
      hql = hql + " and type='" + type + "'";
    }
    if ((startDate != null) && (!"".equals(startDate)) && (endDate != null) && (!"".equals(endDate)))
      hql = hql + " and reportingDate >= to_date('" + startDate + "','yyyy-mm-dd') and reportingDate<=to_date('" + endDate + "','yyyy-mm-dd')";
    else if ((startDate != null) && (!"".equals(startDate)))
      hql = hql + " and reportingDate >= to_date('" + startDate + "','yyyy-mm-dd')";
    else if ((endDate != null) && (!"".equals(endDate))) {
      hql = hql + " and reportingDate<=to_date('" + endDate + "','yyyy-mm-dd')";
    }
    hql = hql + " order by reportingDate desc";
    return this.reportDao.findByPage(hql, page);
  }

  public ReportDao getReportDao() {
    return this.reportDao;
  }

  public void setReportDao(ReportDao reportDao) {
    this.reportDao = reportDao;
  }
}

/* Location:           /Volumes/work/oa/WEB-INF/classes/
 * Qualified Name:     com.jcrj.service.ReportService
 * JD-Core Version:    0.6.0
 */